//
//  ExaminaStatisticsFirstCell.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/22.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "ExaminaStatisticsFirstCell.h"

@implementation ExaminaStatisticsFirstCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *tlabel = [[UILabel alloc] init];
    tlabel.textAlignment = NSTextAlignmentLeft;
    tlabel.textColor = [UIColor colorWithHexString:@"#666666"];
    tlabel.font = [UIFont systemFontOfSize:LoginFont-3];
    _tlabel = tlabel;
    [self.contentView addSubview:self.tlabel];
    
    UIButton *timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [timeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [timeBtn setTitleColor:[UIColor colorWithHexString:@"#ff4949"] forState:UIControlStateNormal];
    [timeBtn.titleLabel setFont:[UIFont systemFontOfSize:LoginFont-3]];
    [timeBtn addTarget:self action:@selector(changeTime:) forControlEvents:UIControlEventTouchUpInside];
    _timeBtn = timeBtn;
    [self.contentView addSubview:self.timeBtn];

    //布局
    WS(ws);
    [self.tlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws).with.offset(25*SCALEUNIT);
        make.width.mas_equalTo(200*SCALEUNIT);
        make.height.centerY.mas_equalTo(ws.contentView);
    }];
    [self.timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(ws.tlabel);
        make.right.mas_equalTo(ws).with.offset(-25*SCALEUNIT);
    }];
}

#pragma mark ==========填充数据==========
- (void)setRow:(NSInteger)row{
    _row =row;
    if (_row == 0) {
        self.tlabel.text = @"起始时间";
        if (self.timeBtn.titleLabel.text==nil) [self.timeBtn setTitle:[BCMHelper dateChangeToString:[self getFirstDayOfThisMonth]] forState:UIControlStateNormal];
    } else {
        self.tlabel.text = @"截至时间";
        if (self.timeBtn.titleLabel.text==nil) [self.timeBtn setTitle:[BCMHelper dateChangeToString:[NSDate date]] forState:UIControlStateNormal];
    }
}

#pragma mark ==========获取当月第一天==========
-(NSDate *)getFirstDayOfThisMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *firstDay;
    [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&firstDay interval:nil forDate:[NSDate date]];
    return firstDay;
}


- (void)changeTime:(UIButton *)btn{
    btn.userInteractionEnabled = NO;
    if (self.block != nil) {
        self.block(btn);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
